<template>
  <div class="comment">
    <el-card>
      <my-header title="评论管理" slot="header"></my-header>
      <transition name="fade">
        <div v-if="tableData.length > 0">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column label="文章标题" prop="title"> </el-table-column>
            <el-table-column label="总评论数" prop="total_comment_count">
            </el-table-column>
            <el-table-column label="粉丝评论数" prop="fans_comment_count">
            </el-table-column>
            <el-table-column label="评论状态" prop="comment_status">
              <template v-slot="scope">
                <el-tag
                  :type="scope.row.comment_status ? 'success' : 'primary'"
                  >{{ scope.row.comment_status ? '正常' : '关闭' }}</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column label="操作" prop="comment_status">
              <template v-slot="scope">
                <el-button
                  :type="scope.row.comment_status ? 'danger' : 'primary'"
                  size="small"
                  @click="changeStatus(scope.row)"
                  >{{
                    scope.row.comment_status ? '关闭评论' : '打开评论'
                  }}</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination">
            <el-pagination
              background
              @current-change="handleCurrentChange"
              :current-page="pagination.currentPage"
              layout="total, prev, pager, next"
              :total="pagination.total"
            >
            </el-pagination>
          </div>
        </div>
      </transition>
    </el-card>
  </div>
</template>

<script>
import { getArticles } from '@/api/articles'
import { comment } from '@/api/comment'
export default {
  name: 'comment',
  data () {
    return {
      tableData: [],
      pagination: {
        currentPage: 1,
        total: 100
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    getData () {
      const data = {
        response_type: 'comment',
        page: this.pagination.currentPage,
        per_page: 10
      }
      getArticles(data).then(res => {
        this.pagination.total = res.data.total_count
        this.tableData = res.data.results
      })
    },

    // 修改当前页码事件
    handleCurrentChange (page) {
      this.pagination.currentPage = page
      this.getData()
    },
    changeStatus (row) {
      const status = row.comment_status
      const id = row.id.toString()
      comment({ article_id: id }, { allow_comment: !status }).then(res => {
        this.$message.success('修改成功!')
        this.getData()
      })
    }
  }
}
</script>
<style lang="less">
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.6s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
.pagination {
  margin-top: 30px;
  text-align: center;
}
</style>
